﻿#include "FITKAcousticsSEAModel.h"
#include "FITKAbstractSEASubSys.h"
#include "FITKAbstractSEAFace.h"
#include "FITKAcousticsDiscNodeList.h"
#include <QSet>


namespace Acoustics
{
    FITKAcousticsSEAModel::FITKAcousticsSEAModel()
    {
        //创建对象
        _subsysMgr = new FITKAcousticsSEASubSysManager;
        _subsysMgr->setParentDataIDM(this->getDataObjectID());
        _faceManager = new FITKSEAFaceManager;
        _faceManager->setParentDataIDM(this->getDataObjectID());
    }


    FITKAcousticsSEAModel::~FITKAcousticsSEAModel()
    {
        if (_subsysMgr) delete _subsysMgr;
        if (_faceManager) delete _faceManager;
    }

    Interface::FITKModelEnum::AbsModelType FITKAcousticsSEAModel::getAbsModelType()
    {
        return Interface::FITKModelEnum::AMTunstructuredMesh;
    }

    FITKAcousticsSEASubSysManager* FITKAcousticsSEAModel::getSEASubSysManager()
    {
        return _subsysMgr;
    }

    Acoustics::FITKSEAFaceManager* FITKAcousticsSEAModel::getSEAFaceManager()
    {
        return _faceManager;
    }

    QList<FITKAbstractSEASubSys*> FITKAcousticsSEAModel::getSEASubsysConnectedWithNodes(const QList<int>& nodeIDs, const bool& common) const
    {
        //错误值
        if (nodeIDs.isEmpty()) return QList<FITKAbstractSEASubSys*>();
        //匿名函数，找出包含节点的子系统
        auto  getSubsys = [&](const int id)->QList<FITKAbstractSEASubSys*>
        {
            QList<FITKAbstractSEASubSys*> sys;
            const int n = _subsysMgr->getDataCount();
            for (int i=0;i<n; ++i)
            {
                //遍历子系统
                FITKAbstractSEASubSys* s = _subsysMgr->getDataByIndex(i);
                if (s && s->isContainsNode(id))
                    sys.append(s);
            }
            return sys;
        };
        //第一个作为子系统求交集或者并集的基础
        QList<FITKAbstractSEASubSys*> baseSys = getSubsys(nodeIDs[0]);
        QSet<FITKAbstractSEASubSys*> baseSet = baseSys.toSet();
        

        for (int i = 1; i < nodeIDs.size(); ++i)
        {
            //一个点的关联子系统
            QList<FITKAbstractSEASubSys*> aSys = getSubsys(nodeIDs[i]);
            //交集
            if (common)
            {
                baseSet = baseSet.intersect(aSys.toSet());
            }
            //并集
            else
            {

                baseSet = baseSet.unite(aSys.toSet());
            }
        }
        return baseSet.toList();
    }

    void FITKAcousticsSEAModel::clearSEAModel()
    {
        if (_subsysMgr) _subsysMgr->clear();
        if (_faceManager) _faceManager->clear();
    }

}


